class Carousel {
  constructor() {
    this.container = document.querySelector('.carousel-slides');
    this.slides = document.querySelectorAll('.carousel-slide');
    this.dots = document.querySelectorAll('.dot');
    this.currentIndex = 0;
    
    this.init();
  }

  init() {
    // 绑定按钮事件
    document.querySelector('.prev').addEventListener('click', () => this.prev());
    document.querySelector('.next').addEventListener('click', () => this.next());
    
    // 绑定指示点事件
    this.dots.forEach((dot, index) => {
      dot.addEventListener('click', () => this.goToSlide(index));
    });

    // 自动播放
    setInterval(() => this.next(), 5000);
  }

  updateSlides() {
    this.container.style.transform = `translateX(-${this.currentIndex * 100}%)`;
    
    // 更新指示点
    this.dots.forEach((dot, index) => {
      dot.classList.toggle('active', index === this.currentIndex);
    });
  }

  next() {
    this.currentIndex = (this.currentIndex + 1) % this.slides.length;
    this.updateSlides();
  }

  prev() {
    this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length;
    this.updateSlides();
  }

  goToSlide(index) {
    this.currentIndex = index;
    this.updateSlides();
  }
}

// 初始化轮播图
document.addEventListener('DOMContentLoaded', () => {
  new Carousel();
}); 